
const { getList, getDetail, newBlog, updateBlog, delBlog } = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../model/resModel')

// 统一的登录验证函数 -- 因为是公用的登录部分
// 主要是拦截未登录的用户
const  loginCheck = (req) => {
    if(!req.session.username){
        return Promise.resolve(
            new SuccessModel('尚未登录')
        )
    }
}

const handleBlogRouter = (req, res) => {
    // const method = req.method  //获取请求方法
    // const url = req.url; //获取到资源路径  --/index.html?name=zhangsan
    // const path = url.split('?')[0]; //文件资源路径 --/index.html
    const id = req.query.id
    // 获取博客列表
    if (req.method === 'GET' && req.path === '/api/blog/list') {
    
        const author = req.query.author || ''
        const keyword = req.query.keyword || ''
        const result = getList(author, keyword)

        return result.then(listData => {
            return new SuccessModel(listData)
        })
        // return new SuccessModel(listData)
    }

    // 获取博客详情
    if (req.method === 'GET' && req.path === '/api/blog/detail') {
        const result = getDetail(id)
        return result.then(data => {
            return new SuccessModel(data)
        })
    }

    // 新建博客
    if (req.method === 'POST' && req.path === '/api/blog/new') {
        // req.body.author = 'zhangsan'
        const loginCheckResult = loginCheck(req,userid)
        if(loginCheckResult){
            // 如果有返回值 -- 说明是未登录
            return loginCheckResult
        }
        // author 是当前的登录用户
        req.body.author = req.session.username
        const result = newBlog(req.body)
        // const data = newBlog(req.body)
        return result.then(data =>{
            return new SuccessModel(data)
        })
        
    }

    // 更新博客
    if (req.method === 'POST' && req.path === '/api/blog/update') {
        
        const loginCheckResult = loginCheck(req)
        if(loginCheckResult){
            // 如果有返回值 -- 说明是未登录
            return loginCheckResult
        }

        const result = updateBlog(id, req.body)
        // if (result) {
        //     return new SuccessModel('更新成功')
        // } else {
        //     return new ErrorModel('更新博客失败')
        // }
        return result.then(val => {
            if(val){
                return new SuccessModel()
            }else{
                return new ErrorModel('更新博客失败')
            }
        })
    }

    // 删除博客
    if (req.method === 'POST' && req.path === '/api/blog/delete') {
        // const author = 'zhangsan'

        const loginCheckResult = loginCheck(req)
        if(loginCheckResult){
            // 如果有返回值 -- 说明是未登录
            return loginCheckResult
        }
        const author = req.session.username
        const result = delBlog(id,author)
        return result.then(val => {
            if(val){
                    return new SuccessModel('删除成功')
                } else {
                    return new ErrorModel('删除失败')
                
            }
        })
        
    }
}

module.exports = handleBlogRouter






